package me.ingxin.android.tools

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.FragmentActivity

/**
 * 找到fragmentActivity
 */
fun Context.getFragmentActivity(): FragmentActivity? {
    if (this is FragmentActivity) {
        return this
    } else if (this is ContextWrapper) {
        return this.baseContext.getFragmentActivity()
    }
    return null
}

/**
 * 找到activity
 */
fun Context.getActivity(): Activity? {
    if (this is Activity) {
        return this
    } else if (this is ContextWrapper) {
        return this.baseContext.getActivity()
    }
    return null
}

/**
 * 打开activity
 */
inline fun <reified T : Activity> Context.startAct(
    options: Bundle? = null,
    noinline putBlock: (Intent.() -> Unit)? = null
) {
    val itt = Intent(this, T::class.java)
    putBlock?.invoke(itt)
    if (options != null) {
        startActivity(itt, options)
    } else {
        startActivity(itt)
    }
}